package com.mshop.fw.util;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Jun 9, 2008 12:35:21 AM
 * @version $Id: FreeMarkerParser.java,v 1.1 2008/03/26 06:24:07 airsou Exp $
 */
public class FreeMarkerParser {
	private static Configuration cfg;
	private static final String _charsetName = "UTF-8";
	static {
		cfg = new Configuration();
		try {
			cfg.setDirectoryForTemplateLoading(new File(Config
					.getString("sys.javaroot")));
		} catch (IOException e) {
			e.printStackTrace();
		}
		cfg.setTemplateUpdateDelay(0);
		cfg
				.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
		cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
		cfg.setDefaultEncoding("UTF-8");
		cfg.setOutputEncoding(_charsetName);
		cfg.setLocale(Locale.CHINA);
		cfg.setNumberFormat("#.#");
		cfg.setDateFormat("yyyy-MM-dd");
		cfg.setTimeFormat("HH:mm:Ss");
		cfg.setDateTimeFormat("yyyy-MM-dd HH:mm:Ss");
		cfg.addAutoImport("model", "/WEB-INF/pages/global_library.ftl");
	}

	public static String substitute(String templateName,
			Map<String, Object> context) {
		return substitute(templateName, context, null);
	}

	public static String substitute(String templateName,
			Map<String, Object> context, String outputEncoding) {
		context.put("now", new Date());
		context.put("domainName", Config.getString("sys.domain"));
		try {
			if (!StringUtil.isEmpty(outputEncoding))
				cfg.setOutputEncoding(outputEncoding);
			Template temp = cfg.getTemplate(templateName);
			StringWriter writer = new StringWriter();
			temp.process(context, writer);
			String restring = writer.toString();
			writer.flush();
			writer.close();
			return restring;
		} catch (Exception ex) {
			System.err.println("Template File: " + templateName);
			ex.printStackTrace();
		} finally {
			if (!StringUtil.isEmpty(outputEncoding))
				cfg.setOutputEncoding(_charsetName);
		}
		return null;
	}

	public static String substitute(String templateName, ActionContext context,
			String outputEncoding) {
		try {
			if (!StringUtil.isEmpty(outputEncoding))
				cfg.setOutputEncoding(outputEncoding);
			Template temp = cfg.getTemplate(templateName);
			StringWriter writer = new StringWriter();
			temp.process(context, writer);
			String restring = writer.toString();
			writer.flush();
			writer.close();
			return restring;
		} catch (Exception ex) {
			System.err.println("Template File: " + templateName);
			ex.printStackTrace();
		} finally {
			if (!StringUtil.isEmpty(outputEncoding))
				cfg.setOutputEncoding(_charsetName);
		}
		return null;
	}

	public static String webSubstitute(String templateName,
			Map<String, Object> context) {
		return substitute("/WEB-INF/pages" + templateName, context);
	}
}